package com.qf.web;

import com.qf.pojo.UserPojo;

import com.qf.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    UserService userService;
    //跳转登陆页面
    @RequestMapping("jump")
    public String jumpLogin(){
        return  "forward:login";
    }

    @RequestMapping("login")
    public String login(UserPojo userPojo,Model model){
        // 现在通过shiro来实现认证
        // Subject: 当前用户；

        Subject currentUser = SecurityUtils.getSubject();
        if (null == currentUser) { // 系统异常；shiro出现问题；
            return "error";
        }
        if (!currentUser.isAuthenticated()) { // 代表当前用户没有认证
            // 要去做认证；需要我们shiro的实体类
            UsernamePasswordToken token = new UsernamePasswordToken(userPojo.getUname(),userPojo.getUpass());
            token.setRememberMe(true);// 记住我
            // 去做登录
            try {
                currentUser.login(token);
            } catch (UnknownAccountException uae) {
                model.addAttribute("errorMsg", "用户不存在");
                return "login";
            } catch (IncorrectCredentialsException ice) {
                model.addAttribute("errorMsg", "密码错误");
                return "login";
            } catch (AuthenticationException ex) { // 用户名或者密码错误
                model.addAttribute("errorMsg", "用户名或者密码错误");
                return "login";
            }
            // 如果用户认证成功，咱们就返回首页
            if (currentUser.isAuthenticated()) {
                return "forward:findall";
            }
        }
        model.addAttribute("errorMsg", "系统繁忙，请稍后再试");
        return "login";
    }
    @RequestMapping("logout")
    public String logout(){
        SecurityUtils.getSubject().logout();
        return "login";
    }
    //增加用户
    @RequestMapping("addUser")
    public String AddUser(UserPojo userPojo){
        userService.addUser(userPojo);
        return "forward:selectAll";
    }
    @RequestMapping("jumpAdd")
    public String jumpAdd(){
        return "userAdd";
    }
    //查询所有用户
    @RequestMapping("selectAll")
    public String selectAllUser(Model model){
        List<UserPojo> userPojoList=  userService.selectAllUser();
        model.addAttribute("userPojoList",userPojoList);
        return "userlist";

    }
    //删除用户
    @RequestMapping("deleteUser")
    public String deleteUser(int uid){
        userService.deleteUser(uid);
        return "forward:selectAll";
    }
    //更新用户
    @RequestMapping("updateUser")
    public String updateUser(UserPojo userPojo){
        userService.updateUser(userPojo);
        return "forward:selectAll";
    }
    @RequestMapping("selectbyid")
    public String selectByid(int uid,Model model){
       UserPojo userPojo= userService.selectById(uid);
       model.addAttribute("user",userPojo);
       return "updateUser";
    }
    /*//跳转至login页
    @RequestMapping("jumplogin1")
    public String jumpLogin1(){
        return "login1";
    }
*/
    /*@RequestMapping("login1")
    public String login1(UserPojo userPojo,Model model){
        // 现在通过shiro来实现认证
        // Subject: 当前用户；

        Subject currentUser = SecurityUtils.getSubject();
        if (null == currentUser) { // 系统异常；shiro出现问题；
            return "error";
        }
        if (!currentUser.isAuthenticated()) { // 代表当前用户没有认证
            // 要去做认证；需要我们shiro的实体类
            UsernamePasswordToken token = new UsernamePasswordToken(userPojo.getUname(),userPojo.getUpass());
            token.setRememberMe(true);// 记住我
            // 去做登录
            try {
                currentUser.login(token);
            } catch (UnknownAccountException uae) {
                model.addAttribute("errorMsg", "用户不存在");
                return "login1";
            } catch (IncorrectCredentialsException ice) {
                model.addAttribute("errorMsg", "密码错误");
                return "login1";
            } catch (AuthenticationException ex) { // 用户名或者密码错误
                model.addAttribute("errorMsg", "用户名或者密码错误");
                return "login1";
            }
            // 如果用户认证成功，咱们就返回首页
            if (currentUser.isAuthenticated()) {
                return "forward:selectAll";
            }
        }
        model.addAttribute("errorMsg", "系统繁忙，请稍后再试");
        return "login1";
    }*/
}
